package com.jy.importclient.handler;

import com.jy.importclient.common.dto.ResDto;
import com.jy.importclient.common.dto.StartTaskDto;
import com.jy.importclient.common.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @类名 RequestMethods
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/4/25 21:34
 * @版本 1.0
 */
@Slf4j
public abstract class AbsReqSqlldrHandler {

    protected abstract ResDto createTask(String sqlldrUrl);

    protected abstract ResDto upload(String sqlldrUrl, String path, String api);

    protected abstract ResDto startTask(String sqlldrUrl, StartTaskDto startTaskDto);

    protected abstract ResDto checkResult(String sqlldrUrl, String taskId);

    protected abstract String downloads(String downloadFilePath, String sqlldrUrl, String file) throws IOException;

    public Map<String, Object> execution(String downloadFilePath, String ctlPath, String datPath, String sqlldrUrl, StartTaskDto startTaskDto) throws IOException {
        boolean flag = true;
        //1.创建任务
        ResDto createTask = this.createTask(sqlldrUrl);
        if (createTask.getCode() != 1) {
            log.warn("创建任务：{}", createTask.getMsg());
            flag = false;
        }

        //获取taskId
        Map<String, Object> createTaskMap = ConvertUtil.objectConvertMap(createTask.getData());
        String taskId = (String) createTaskMap.get("taskId");
        String ctlApi = (String) createTaskMap.get("ctlLoad");
        String datApi = (String) createTaskMap.get("datLoad");

        //2.文件上传
        ResDto upload = this.upload(sqlldrUrl, ctlPath, ctlApi);
        if (upload.getCode() != 1) {
            log.warn("ctl文件上传：{}", upload.getMsg());
            flag = false;
        }

        upload = this.upload(sqlldrUrl, datPath, datApi);
        if (upload.getCode() != 1) {
            log.warn("dat文件上传：{}", upload.getMsg());
            flag = false;
        }

        //3.启动ctl执行
        startTaskDto.setTaskId(taskId);
        ResDto startTask = this.startTask(sqlldrUrl, startTaskDto);
        if (startTask.getCode() != 1) {
            log.warn("启动ctl执行：{}，任务id是：{}", startTask.getMsg(), taskId);
            flag = false;
        }

        //4.检查ctl执行
        ResDto checkResult = this.checkResult(sqlldrUrl, taskId);
        if (checkResult.getCode() != 1) {
            log.warn("检查ctl执行：失败，任务id是：{}", taskId);
            flag = false;
        }

        Map<String, Object> checkResultMap = ConvertUtil.objectConvertMap(checkResult.getData());
        String filePath = null;
        Object logfileObj = checkResultMap.get("logfile");
        Object badfileObj = checkResultMap.get("badfile");
        Object discardfileObj = checkResultMap.get("discardfile");
        if (!StringUtils.isEmpty(logfileObj)) {
            filePath = (String) logfileObj;
        } else if (!StringUtils.isEmpty(badfileObj)) {
            filePath = (String) badfileObj;
        } else if (!StringUtils.isEmpty(discardfileObj)) {
            filePath = (String) discardfileObj;
        }

        //5.文件下载
        filePath = this.downloads(downloadFilePath, sqlldrUrl, filePath);

        //返回参数
        Map<String, Object> result = new HashMap<>();
        result.put("filePath", filePath);
        result.put("flag", flag);
        return result;
    }
}
